package com.jinwei.qunguanjia.web.group.controller;

import com.jinwei.qunguanjia.common.core.domain.Response;
import com.jinwei.qunguanjia.common.core.domain.model.LoginUser;
import com.jinwei.qunguanjia.common.core.page.PageDataInfo;
import com.jinwei.qunguanjia.common.core.redis.RedisCache;
import com.jinwei.qunguanjia.common.utils.*;
import com.jinwei.qunguanjia.user.dto.GroupCommentDTO;
import com.jinwei.qunguanjia.user.dto.GroupCommentReplyDTO;
import com.jinwei.qunguanjia.user.dto.SensitiveWordDTO;
import com.jinwei.qunguanjia.user.service.GroupCommentReplyService;
import com.jinwei.qunguanjia.user.service.GroupCommentService;
import com.jinwei.qunguanjia.user.service.SensitiveWordService;
import com.jinwei.qunguanjia.user.service.UserTokenService;
import com.jinwei.qunguanjia.web.group.vo.GroupCommentReplyVO;
import com.jinwei.qunguanjia.web.group.vo.GroupCommentVO;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

/**
 * 群评论管理相关接口
 *
 * @author yuanzs
 */
@RestController
@RequestMapping("/groupComment")
public class GroupCommentController {

    @Resource
    private GroupCommentService groupCommentService;

    @Resource
    private SensitiveWordService sensitiveWordService;

    @Resource
    private GroupCommentReplyService groupCommentReplyService;

    @Resource
    private RedisCache redisCache;

    @Resource
    private UserTokenService tokenService;

    /**
     * 发布评论接口
     * @param groupCommentVO 对象
     */
    @PostMapping("/save")
    public Response save(@Validated @RequestBody GroupCommentVO groupCommentVO) throws IllegalAccessException {
        List<SensitiveWordDTO> sensitiveWordList = sensitiveWordService.list(new SensitiveWordDTO());
        for (SensitiveWordDTO sensitiveWord : sensitiveWordList) {
            if (groupCommentVO.getComment().indexOf(sensitiveWord.getSensitiveWordContent()) > -1) {
                groupCommentVO.setComment(groupCommentVO.getComment().replaceAll(sensitiveWord.getSensitiveWordContent(), "*"));
            }
        }
        GroupCommentDTO groupCommentDTO = new GroupCommentDTO();
        BeanUtils.copyProperties(groupCommentVO, groupCommentDTO);
        groupCommentDTO.setUserId(SystemContextUtils.getUserId());
        groupCommentService.save(groupCommentDTO);
        return Response.success();
    }

    /**
     * 获取群评论分页列表
     * @param groupId 群id
     */
    @GetMapping("/listPage")
    public PageDataInfo<GroupCommentVO> list(String groupId, HttpServletRequest request) throws ParseException, IllegalAccessException {
        GroupCommentDTO groupCommentDTO = new GroupCommentDTO();
        groupCommentDTO.setGroupId(groupId);
        groupCommentDTO.setCommentStatus("0");
        String token = tokenService.getToken(request);
        LoginUser loginUser = redisCache.getCacheObject(tokenService.getTokenKey(token));
        if (null != loginUser) {
            groupCommentDTO.setUserId(loginUser.getUserId());
        }
        PageUtils.startPage();
        PageDataInfo<GroupCommentDTO> pageDataInfo = groupCommentService.listPage(groupCommentDTO);
        List<GroupCommentDTO> list = pageDataInfo.getData();
        List<GroupCommentVO>  groupCommentVOs = new ArrayList<>();
        List<String> groupCommentIdList = new ArrayList<>();
        for (GroupCommentDTO groupCommentDTO2 : list) {
            groupCommentIdList.add(groupCommentDTO2.getGroupCommentId());
            GroupCommentVO groupCommentVO = new GroupCommentVO();
            BeanUtils.copyProperties(groupCommentDTO2, groupCommentVO);
            groupCommentVO.setCreateTimeUtile(DateUtils.timeUtile(DateUtils.stringToDateTime(groupCommentDTO2.getCreateTime())));
            groupCommentVOs.add(groupCommentVO);
        }
        //查询评论回复列表
        GroupCommentReplyDTO groupCommentReplyDTO = new GroupCommentReplyDTO();
        groupCommentReplyDTO.setGroupCommentIdList(groupCommentIdList);
        if (null != loginUser) {
            groupCommentReplyDTO.setUserId(loginUser.getUserId());
        }
        List<GroupCommentReplyDTO> groupCommentReplyDTOS = groupCommentReplyService.list(groupCommentReplyDTO);
        for (GroupCommentVO groupCommentVO1 : groupCommentVOs) {
            List<GroupCommentReplyVO> groupCommentReplyList = new ArrayList<>();
            for (GroupCommentReplyDTO groupCommentReplyDTO1 : groupCommentReplyDTOS) {
                if (groupCommentVO1.getGroupCommentId().equals(groupCommentReplyDTO1.getGroupCommentId())) {
                    GroupCommentReplyVO groupCommentReplyVO = new GroupCommentReplyVO();
                    BeanUtils.copyProperties(groupCommentReplyDTO1, groupCommentReplyVO);
                    groupCommentReplyList.add(groupCommentReplyVO);
                }
            }
            groupCommentVO1.setGroupCommentReplyList(groupCommentReplyList);
        }
        PageDataInfo<GroupCommentVO> pageDataInfoVO = new PageDataInfo<>();
        BeanUtils.copyProperties(pageDataInfo, pageDataInfoVO);
        pageDataInfoVO.setData(groupCommentVOs);
        return pageDataInfoVO;
    }
}
